<?php

declare(strict_types=1);

namespace app\controller\system\order;

use app\BaseController;
use app\model\ShipmentCompany as ShipmentCompanyModel;
use app\service\factory\ResponseFactory;
use app\service\order\ShipmentCompanyService;
use app\validate\order\ShipmentCompanyValidate;
use InvalidArgumentException;
use think\Response;
use Throwable;

class ShipmentCompany extends BaseController
{
    public function index(ShipmentCompanyService $service): Response
    {
        try {
            $page = max(1, (int) $this->request->param('page', 1));
            $pageSize = (int) $this->request->param('page_size', 20);
            $pageSize = $pageSize > 0 ? min($pageSize, 100) : 20;

            $filters = $this->request->only(['keyword', 'status'], 'get');

            $paginator = $service->paginate($filters, $page, $pageSize);
            $list = $service->formatCollection($paginator->items());

            return json(ResponseFactory::paginated(
                $list,
                (int) $paginator->total(),
                $page,
                $pageSize
            ));
        } catch (Throwable $e) {
            return json(ResponseFactory::error($e->getMessage(), $e->getCode() ?: 500));
        }
    }

    public function read(int $id, ShipmentCompanyService $service): Response
    {
        try {
            $company = $service->findById($id);
            if (!$company) {
                return json(ResponseFactory::error('物流公司不存在', 404));
            }

            return json(ResponseFactory::success($service->format($company)));
        } catch (Throwable $e) {
            return json(ResponseFactory::error($e->getMessage(), $e->getCode() ?: 500));
        }
    }

    public function save(ShipmentCompanyService $service): Response
    {
        try {
            $payload = $this->request->post();
            $this->validate($payload, ShipmentCompanyValidate::class . '.create');

            $company = $service->create($payload);

            return json(ResponseFactory::success($service->format($company), '物流公司已创建'));
        } catch (InvalidArgumentException $e) {
            return json(ResponseFactory::error($e->getMessage(), 422));
        } catch (Throwable $e) {
            return json(ResponseFactory::error($e->getMessage(), $e->getCode() ?: 500));
        }
    }

    public function update(int $id, ShipmentCompanyService $service): Response
    {
        try {
            $company = ShipmentCompanyModel::find($id);
            if (!$company) {
                return json(ResponseFactory::error('物流公司不存在', 404));
            }

            $payload = $this->request->put();
            $payload['id'] = $id;

            $this->validate($payload, ShipmentCompanyValidate::class . '.update');

            $updated = $service->update($company, $payload);

            return json(ResponseFactory::success($service->format($updated), '物流公司已更新'));
        } catch (InvalidArgumentException $e) {
            return json(ResponseFactory::error($e->getMessage(), 422));
        } catch (Throwable $e) {
            return json(ResponseFactory::error($e->getMessage(), $e->getCode() ?: 500));
        }
    }

    public function delete(int $id, ShipmentCompanyService $service): Response
    {
        try {
            $company = ShipmentCompanyModel::find($id);
            if (!$company) {
                return json(ResponseFactory::error('物流公司不存在', 404));
            }

            if (!$service->delete($company)) {
                return json(ResponseFactory::error('删除失败', 500));
            }

            return json(ResponseFactory::success(true, '物流公司已删除'));
        } catch (Throwable $e) {
            return json(ResponseFactory::error($e->getMessage(), $e->getCode() ?: 500));
        }
    }
}
